package com.nnnu.wsnackshop.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nnnu.wsnackshop.exception.ObjectException;
import com.nnnu.wsnackshop.pojo.dto.AddBrandDTO;
import com.nnnu.wsnackshop.pojo.dto.BrandPageDTO;
import com.nnnu.wsnackshop.pojo.dto.UpdateBrandDTO;
import com.nnnu.wsnackshop.pojo.entity.ProductBrands;
import com.nnnu.wsnackshop.mapper.ProductBrandsMapper;
import com.nnnu.wsnackshop.pojo.vo.BrandVO;
import com.nnnu.wsnackshop.result.PageResult;
import com.nnnu.wsnackshop.service.IProductBrandsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 品牌表 服务实现类
 * </p>
 *
 * @author zk
 * @since 2025-05-14
 */
@Service
@RequiredArgsConstructor
public class ProductBrandsServiceImpl extends ServiceImpl<ProductBrandsMapper, ProductBrands> implements IProductBrandsService {

    @Override
    public PageResult<BrandVO> pageBrands(@NotNull BrandPageDTO dto) {
        Page<ProductBrands> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        LambdaQueryWrapper<ProductBrands> qw = Wrappers.<ProductBrands>lambdaQuery()
                .like(StrUtil.isNotBlank(dto.getName()), ProductBrands::getName, dto.getName())
                .eq(dto.getStatus() != null, ProductBrands::getStatus, dto.getStatus())
                .orderByDesc(ProductBrands::getUpdatedAt);
        Page<ProductBrands> result = baseMapper.selectPage(page, qw);

        List<BrandVO> vos = result.getRecords().stream().map(entity -> BeanUtil.copyProperties(entity, BrandVO.class)).collect(Collectors.toList());

        return PageResult.<BrandVO>builder()
                .total(result.getTotal())
                .items(vos)
                .build();
    }

    @Override
    public void addBrand(@NotNull AddBrandDTO dto) {
        boolean exists = baseMapper.exists(Wrappers.<ProductBrands>lambdaQuery()
                .eq(ProductBrands::getName, dto.getName()));
        if (exists) {
            throw new ObjectException("品牌名称已存在");
        }
        ProductBrands entity = BeanUtil.copyProperties(dto, ProductBrands.class);
        entity.setStatus(dto.getStatus() == null ? 1 : dto.getStatus());
        baseMapper.insert(entity);
    }

    @Override
    public void updateBrand(@NotNull UpdateBrandDTO dto) {
        ProductBrands entity = baseMapper.selectById(dto.getId());
        if (entity == null) {
            throw new ObjectException("品牌不存在: " + dto.getId());
        }
        boolean exists = baseMapper.exists(Wrappers.<ProductBrands>lambdaQuery()
                .eq(ProductBrands::getName, dto.getName())
                .ne(ProductBrands::getId, dto.getId()));
        if (exists) {
            throw new ObjectException("品牌名称已存在");
        }
        entity.setName(dto.getName());
        entity.setDescription(dto.getDescription());
        entity.setLogoUrl(dto.getLogoUrl());
        entity.setStatus(dto.getStatus());
        baseMapper.updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBrands(List<Integer> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            throw new ObjectException("删除ID列表不能为空");
        }
        baseMapper.deleteBatchIds(ids.stream().distinct().toList());
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        ProductBrands entity = baseMapper.selectById(id);
        if (entity == null) {
            throw new ObjectException("品牌不存在: " + id);
        }
        entity.setStatus(status);
        baseMapper.updateById(entity);
    }

    @Override
    public BrandVO getBrandDetail(Integer id) {
        ProductBrands entity = baseMapper.selectById(id);
        if (entity == null) {
            throw new ObjectException("品牌不存在: " + id);
        }
        return BeanUtil.copyProperties(entity, BrandVO.class);
    }
}
